<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021/5/21 0021
 * Time: 下午 2:56
 */
namespace app\index\controller;
use app\index\controller\BaseCompanyUser;
use app\model\ActivityCompany;
use app\model\CompanyInvite;
use app\model\Job;
use app\model\Places;
use app\model\UserCompanyInvite;
use app\model\UserJobSend;

class UserCompany extends  BaseCompanyUser{

    /**
     * 保存企业用户资料
     */
    function save_user_company(){
        $params=request()->param();

        $v_arr=[
            ['key'=>'company_name','null_del'=>'公司名字不能为空'],
            ['key'=>'introduce','null_del'=>'公司介绍不能为空'],
            ['key'=>'logo','null_del'=>'公司logo不能为空'],
            ['key'=>'company_pic','null_del'=>'公司照片不能为空'],
            ['key'=>'enterprise_certification_pic','null_del'=>'企业资质图片不能为空'],
            ['key'=>'enterprise_type','null_del'=>'请选择企业性质'],
            ['key'=>'industry','null_del'=>'从事行业不能为空'],
            ['key'=>'scale_type','null_del'=>'选择企业规模'],
            ['key'=>'area_id','null_del'=>'请选择地区'],
            ['key'=>'address','null_del'=>'输入地址'],
            ['key'=>'link_name','null_del'=>'联系人不能为空'],
            ['key'=>'link_mobile','null_del'=>'联系电话不能为空'],
            ['key'=>'company_email','null_del'=>'企业邮箱不能为空'],
            //['key'=>'x','null_del'=>'地图x坐标不能为空'],
            //['key'=>'y','null_del'=>'地图y坐标不能为空'],
        ];
        unset($params['enterprise_type_name']);
        unset($params['scale_type_type_name']);
        unset($params['city_name']);
        unset($params['province_name']);
        $tip_arr=arr_null_deal($params,$v_arr);
        if($tip_arr['code']==1){
            return $this->apiJosn([],201,$tip_arr['tip']);
        }
        $params['user_id']=$this->get_user_id();
        $UserCompanyModel=new \app\model\UserCompany();

        $Places=new Places();
        //var_dump($params['area_id']);
        $city=$Places->get_name_by_id($params['area_id']);
        //var_dump($city);
        $address=$city.$params['address'];
        //var_dump($address);
        $r=get_x_y_by_address($address);
        //var_dump($r);
        if($r['code']==0){
            $params['x']=$r['res']['lng'];
            $params['y']=$r['res']['lat'];
        }else{
            return $this->apiJosn([],201,$r['tip']);
        }


        $r=$UserCompanyModel->save_user_company($params);
        if($r){
            return $this->apiJosn([],200);
        }else{
            return $this->apiJosn([],201,'操作失败');
        }


    }

    /**
     * 取得企业资料
     */
    function get_user_company(){
        $UserCompanyModel=new \app\model\UserCompany();
        $info=$UserCompanyModel->get_user_company($this->get_user_id());
        return $this->apiJosn($info);
    }

    /**
     * 发布公司工作
     */
    function send_job(){
        $params=request()->param();
        $v_arr=[
            ['key'=>'job_name','null_del'=>'职位名称不能为空'],
            ['key'=>'job_type','null_del'=>'选择职位类别'],
            ['key'=>'treatment_type','null_del'=>'选择月薪待遇'],
            ['key'=>'salary_range_type','null_del'=>'选择范薪资围'],
            ['key'=>'department_type','null_del'=>'选择部门'],
            ['key'=>'recruiting_num','null_del'=>'填写招聘人数'],
            ['key'=>'work_experience_type','null_del'=>'选择工作经验'],
            ['key'=>'edu_type','null_del'=>'选择学历'],
            ['key'=>'age_type','null_del'=>'选择年龄类型'],
            ['key'=>'send_day','null_del'=>"填写天发布数"],
            ['key'=>'job_des','null_del'=>'岗位描述不能为空'],
            ['key'=>'highlight_type','null_del'=>'选择亮点福利'],
            ['key'=>'area_id','null_del'=>'选择工作地区'],
            ['key'=>'interview_address','null_del'=>'面试地址'],
            ['key'=>'link_name','null_del'=>'联系人不能为空'],
            ['key'=>'link_mobile','null_del'=>'联系人电话不能为空'],
            ['key'=>'link_email','null_del'=>'收简历邮箱不能为空'],
            ['key'=>'sex_type','null_del'=>'请选择性别']

        ];
        $tip_arr=arr_null_deal($params,$v_arr);
        if($tip_arr['code']==1){
            return $this->apiJosn([],201,$tip_arr['tip']);
        }
        $Job=new Job();
        $params['user_id']=$this->get_user_id();
        unset($params['province_name']);
        unset($params['city_name']);
        $r=$Job->add_job($params);
        if($r){
            return $this->apiJosn([],200);
        }else{
            return $this->apiJosn([],201,'操作失败');
        }

    }

    /**
     * 取得企业用户已经发布的职位
     */
    function get_job_list(){
        $params=request()->param();
        $JobModel=new Job();
        if($params['page']==false){
            $params['page']=1;
        }
        $list=$JobModel->get_job_list_by_user_id($params['page'],$this->get_user_id(),$params);
        return $this->apiJosn($list,200);
    }

    function get_job_detail(){
        $params=request()->param();
        if($params['id']==false){
            return $this->apiJosn([],201,'工作ID不能为空');
        }
        $JobModel=new Job();
        $info=$JobModel->get_job_info_by_id($params['id']);
        return $this->apiJosn($info,200);
    }

    /**
     * 取得已经收到的简历
     */
    function get_has_deliver_resume(){
        $params=request()->param();
        if($params['page']==false){
            $params['page']=1;
        }



        $UserJobSend=new UserJobSend();
        $list=$UserJobSend->get_has_deliver_resume($params['page'],$this->get_user_id());
        return $this->apiJosn($list,200);

    }

    /**
     * 根据投递记录ID,返回对应的投递人简历
     */
    function get_has_deliver_resume_info(){
        $params=request()->param();
        if($params['user_job_send_id']==false){
            return $this->apiJosn([],201,'user_job_send_id不能为空');
        }
        $UserJobSend=new UserJobSend();
        $UserJobSend->if_save_check($params['user_job_send_id']);
        $info=$UserJobSend->get_has_deliver_resume_by_id($params['user_job_send_id']);
        return $this->apiJosn($info);
    }

    /**
     * 企业发送邀请面试
     */
    function send_invite_interview(){
        $params=request()->param();

        $v_arr=[
            ['key'=>'user_job_send_id','null_del'=>'user_job_send_id不能为空'],
            ['key'=>'invite_content','null_del'=>'邀请内容不能为空'],
            ['key'=>'interview_date','null_del'=>'面试时间不能为空'],
            ['key'=>'interview_area','null_del'=>'面试地点不能为空'],
            ['key'=>'link_name','null_del'=>'联系人不能为空'],
            ['key'=>'link_mobile','null_del'=>'联系手机不能为空'],


        ];
        $t_arr=arr_null_deal($params,$v_arr);
        if($t_arr['code']==1){
            return $this->apiJosn([],201,$t_arr['tip']);
        }

        $CompanyInvite=new CompanyInvite();
        $params['user_id']=$this->get_user_id();
        $UserJobSend=new UserJobSend();
        $params['user_id2']=$UserJobSend->get_user_id_by_id($params['user_job_send_id']);

        $UserJobSend=new UserJobSend();



        $r=$CompanyInvite->add_invite($params);
        if($r){
            return $this->apiJosn([],200);
        }else{
            return $this->apiJosn([],201);
        }





    }

    /**
     * 添加一样用户参加指定招聘活动记录
     */
    function add_join_activity_log(){
            $params=request()->param();
            if($params['activity_id']==false){
                return $this->apiJosn([],201,'activity_id不能为空');
            }
            $ActivityCompany=new ActivityCompany();
            $UserCompany=new \app\model\UserCompany();
            $company_id=$UserCompany->get_id_by_user_id($this->get_user_id());

            $if_join=$ActivityCompany->if_join_activity($params['activity_id'],$company_id);
            if($if_join){
                return $this->apiJosn([],201,'不能重复参加活动');
            }

            $r=$ActivityCompany->add_join_activity_log($params['activity_id'],$company_id);
            if($r){
                return $this->apiJosn([],200);
            }else{
                return $this->apiJosn([],201,"操作失败");
            }
    }


    /**
     *  发送公司邀请
     */
    function send_invite(){
        $params=request()->param();
        if($params['user_id']==false){
            return $this->apiJosn([],201,'邀请用户ID不能为空');
        }
        if($params['job_id']==false){
            return $this->apiJosn([],201,'工作ID不能为空');
        }
        if($params['content']==false){
            return $this->apiJosn([],201,'邀请内容不能为空');
        }
        $UserCompanyInvite=new UserCompanyInvite();
        $params['company_user_id']=$this->get_user_id();
        $r=$UserCompanyInvite->addInvite($params);
        if($r){
            return $this->apiJosn([],200);

        }else{
            return $this->apiJosn([],201,'操作失败');
        }


    }

    /**
     * 返回公司里所有的工作
     */
    function get_company_job_key(){
        $Job=new Job();
        $arrs=$Job->get_all_job_key($this->get_user_id());
        return $this->apiJosn($arrs);
    }





}